/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ch.benjaminschuele.defytweaks;

import java.io.DataOutputStream;
import java.io.IOException;

import android.util.Log;

public class OsTreat {
    private static final String TAG = "DefyTweaks";
	private String mStringXY;
	private String mBinaryPath = "/system/bin/";
	private String mSu = mBinaryPath + "su ";
	private String mDd = mBinaryPath + "dd ";
	private String mMv = mBinaryPath + "mv ";
	
	public void SetStringXY(String str)
	{
		mStringXY = str;
	}
	
	public String GetStringXY() 
	{
		return mStringXY;
	}
	
	public void TestSu()
	{
		
		try 
		{
			Log.d(TAG,"Test su start");
			
			// You previously found out which is the right path for the su binary,
			// being it /system/bin/su or /system/xbin/su
			Process suProcess = Runtime.getRuntime().exec(mSu);
			DataOutputStream dos = new DataOutputStream(suProcess.getOutputStream());
			
			//mv exising? to bak
			dos.writeBytes(mMv + "/sdcard/tmp/recovery.img /sdcard/tmp/recovery.img.bak\n");
			//write recovery image to sdcard
			dos.writeBytes(mDd + "if=/dev/block/mmcblk1p16 of=/sdcard/tmp/recovery.img\n");
			
			//dos.writeBytes("flash_image boot " + sdcardPath + "2.img\n");
			//dos.flush();
			//dos.writeBytes("cp " +sdcardPath + "2.ko /system/lib/modules/wlan.ko\n");
			//dos.flush();
			
			dos.writeBytes("exit\n");
			dos.flush();
			suProcess.waitFor();
			
			Log.d(TAG,"Test su stop");
		}
		catch (Exception e) 
		{
			Log.d(TAG,"Test su exception" + e.getMessage());
			e.printStackTrace();
		}	
	}
	
	public void TestSu2()
	{
		Log.d(TAG,"Test su start");
		final Runtime runtime = Runtime.getRuntime();
		
		try 
		{
			runtime.exec("su");
		}
		catch (IOException e) 
		{
			Log.d(TAG,"Test su exception" + e.getMessage());
			e.printStackTrace();
		}
		Log.d(TAG,"Test su end");
	}

}
